// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current:0,
    food_current:0,
    merchant:[],
    menu:[],
    food:[],
    quantity:0,
    hidell: true,
    cart:{
      count: 0,
      total: 0,
      list: {} 
    }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this;
    wx.request({
      url: 'https://www.easy-mock.com/mock/59f696be70d0b32d6619c451/waimai/getStoreType',
      data: {
        'storeId': 1
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        t.setData({
          menu:res.data.data
        })
        t.just(res.data.data[0])
      }
    }) 
  },
  //横向页面切换
  ch_current: function (e) {
    var th = this;
    var index = e.currentTarget.dataset.index;
    th.setData({
      current: index      
    })
  },
  //纵向页面切换
 ch_fd_current: function (e) {
   var th = this;
   var index = e.target.dataset.index;
   var f_type = e.target.dataset.food;
  //  console.log(f_type);
    th.just(f_type)
   th.setData({
     food_current:index
   })

  },
 
  //显示遮罩层
  showcart: function(){
     var th = this;
     console.log(th.data.cart);
     th.setData({
       hidell:false,     
     })  
  },

  test: function(){
    var t = this;
    console.log(t.data.shop_list)
  },

  //隐藏遮罩层
  hidd:function (){
    var th = this;
    th.setData({
      hidell: true
    })  
  },
  //商家数据获取
  merchant: function(){
    var th = this;
    wx.request({
      url: 'https://www.easy-mock.com/mock/59f696be70d0b32d6619c451/waimai/nearStores', 
      data: {
       
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {        
        th.setData({
          merchant:res.data.data
        })
      }
    })
  },
  //获取详细菜单
  just: function(f_type) {
    var that = this;
    wx.request({
      url: 'https://www.easy-mock.com/mock/59f696be70d0b32d6619c451/waimai/getMenu',
      data: {
        'storeId': 1,
        'foodType':f_type
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
       that.setData({
         food:res.data.data
       })
      }
    }) 
  },
  tapaddcart: function(e){
    this.addcart(e.target.dataset.id)
  },
  //加入购物车
  addcart: function (id) {
    console.log(id)
    var num = this.data.cart.list[id] || 0
    this.data.cart.list[id] = num +1
    this.countcart()
  },
  countcart: function(){
    var count = 0,
      total = 0;
    for (var id in this.data.cart.list) {
      var goods = this.data.food[id]
      count += this.data.cart.list[id]
      total += goods.foodPrices * this.data.cart.list[id]
    }
    this.data.cart.count = count
    this.data.cart.total = total
    this.setData({
      cart: this.data.cart
    });
    console.log(this.data.cart)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})